package com.sc.nft.dao;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sc.nft.dto.MallOrderRefundDTO;
import com.sc.nft.entity.MallOrderRefund;
import com.sc.nft.entity.dto.AdminPageRefundOrderDTO;
import com.sc.nft.entity.dto.RefundOrderLogDTO;
import com.sc.nft.enums.MallRefundStatusEnum;
import com.sc.nft.mapper.MallOrderRefundMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 售后订单DAO
 *
 * @author liGen
 * @version 1.0
 * @date 2023/11/15 1:35 下午
 */
@Slf4j
@Repository
public class MallOrderRefundDao extends ServiceImpl<MallOrderRefundMapper, MallOrderRefund> {

    /**
     * 获取订单的有效金额售后订单
     *
     * @param orderId
     * @return
     */
    public List<MallOrderRefund> getAmountInForceListByOrderId(Long orderId) {
        LambdaQueryWrapper<MallOrderRefund> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(MallOrderRefund::getOrderId, orderId);
        wrapper.in(MallOrderRefund::getRefundStatus, MallRefundStatusEnum.getAmountInForceStatusList());
        return baseMapper.selectList(wrapper);
    }

    /**
     * 订单是否存在售后中的售后单
     *
     * @param orderId
     * @return
     */
    public Boolean whetherTheAfterSalesOrderExists(Long orderId) {
        LambdaQueryWrapper<MallOrderRefund> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(MallOrderRefund::getOrderId, orderId);
        wrapper.in(MallOrderRefund::getRefundStatus, MallRefundStatusEnum.afterSalesStatusList());
        Integer result = baseMapper.selectCount(wrapper);
        return null != result && result > 0;
    }

    /**
     * 获取订单的所有售后订单
     *
     * @param orderId
     * @return
     */
    public List<MallOrderRefundDTO> getRefundListByOrderId(Long orderId) {
        return baseMapper.getRefundListByOrderId(orderId);
    }

    /**
     * 获取订单的退款成功的售后订单
     *
     * @param orderId
     * @return
     */
    public List<MallOrderRefund> getRefundSuccessListByOrderId(Long orderId) {
        LambdaQueryWrapper<MallOrderRefund> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(MallOrderRefund::getOrderId, orderId);
        wrapper.eq(MallOrderRefund::getRefundStatus, MallRefundStatusEnum.REFUND_SUCCES);
        return baseMapper.selectList(wrapper);
    }

    /**
     * @param mallRefundStatusEnum
     * @return
     */
    public List<MallOrderRefund> getListByStatus(MallRefundStatusEnum mallRefundStatusEnum) {
        LambdaQueryWrapper<MallOrderRefund> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(MallOrderRefund::getRefundStatus, mallRefundStatusEnum);
        return baseMapper.selectList(wrapper);
    }

    /**
     * 获取用户的所有售后订单
     *
     * @param userId
     * @return
     */
    public Page<MallOrderRefundDTO> getRefundListByUserId(Long userId, Page page) {
        return baseMapper.getRefundListByUserId(page, userId);
    }

    /**
     * 获取售后详情
     *
     * @param id
     * @return
     */
    public MallOrderRefundDTO getDetailsById(Long id) {
        return baseMapper.getDetailsById(id);
    }

    /**
     * 管理后台售后列表
     *
     * @param pageNo
     * @param pageSize
     * @param orderNo
     * @param refundNo
     * @param refundStatus
     * @param refundType
     * @param userTel
     * @return
     */
    public Page<AdminPageRefundOrderDTO> getAdminPageRefundOrderDTO(Integer pageNo, Integer pageSize, String orderNo, String refundNo,
                                                                    Integer refundStatus, Integer refundType, String userTel) {
        return baseMapper.getAdminPageRefundOrderDTO(new Page(pageNo, pageSize), orderNo, refundNo, refundStatus, refundType, userTel);
    }

    /**
     * 售后详情
     *
     * @param id
     * @return
     */
    public AdminPageRefundOrderDTO getAdminRefundOrderDTO(Long id) {
        return baseMapper.getAdminRefundOrderDTO(id);
    }

    /**
     * 通过售后编号获取售后单
     *
     * @param refundNo 售后编号
     * @return
     */
    public MallOrderRefund getByNo(String refundNo) {
        LambdaQueryWrapper<MallOrderRefund> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(MallOrderRefund::getRefundNo, refundNo);
        return baseMapper.selectOne(wrapper);
    }

    /**
     * 获取售后单的状态操作时间
     *
     * @param refundStatus 售后单状态
     * @param changeType   操作类型
     * @return
     */
    public List<RefundOrderLogDTO> getRefundOrderLogDTOList(Integer refundStatus, String changeType) {
        return baseMapper.getRefundOrderLogDTOList(refundStatus, changeType);
    }

    /**
     * 获取分享团的售后单
     *
     * @param teamId
     * @return
     */
    public List<MallOrderRefund> getListByTeamOrder(Long teamId) {
        return baseMapper.getListByTeamOrder(teamId);
    }
}